import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import { fileURLToPath } from "url";
import { viteMockServe } from "vite-plugin-mock";
import { loadEnv } from "vite";
import process from "process";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// https://vite.dev/config/
export default ({ mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd());

  return defineConfig({
    plugins: [
      react(),
      viteMockServe({
        mockPath: "mock",
        // 从环境变量获取mock配置
        localEnabled: env.VITE_APP_USE_MOCK === "true",
        prodEnabled: false,
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    server: {
      // 代理配置
      proxy: {
        "/api": {
          target: env.VITE_API_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  });
};
